package com.fish.web.product;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fish.common.constant.Constant;
import com.fish.common.constant.QueryPage;
import com.fish.common.web.BaseAction;
import com.fish.product.dao.ProductDAO;
import com.fish.product.po.Product;
import com.fish.service.product.ProductService;

public class ProductAction extends BaseAction {
	private static final long serialVersionUID = -8049062515826731018L;
	private static final Log loger = LogFactory.getLog(ProductDAO.class);
	private ProductService productService;
	private Product product = new Product();
	private List<Product> productList = new ArrayList<Product>();
	private QueryPage queryPage = new QueryPage();
	
	public String goToProduct(){
		 return SUCCESS;
	}
	public String goToProductList(){
		if(!getSessionUser().isAdmin()){
			product.setUserId(getSessionUser().getUserId());
		}
		Long count = productService.queryProductCountByObject(product);
		if(count > 0){
			queryPage = new QueryPage(count, Constant.DEFAULT_PAGE_SIZE, queryPage.getCurPage());
		}		
		productList = productService.queryProductByObject(product,queryPage);
		return SUCCESS;
	}

	public String viewProductInfo(){
		if(StringUtils.isNotBlank(product.getProductId())) {
			product = productService.queryProductByPrimaryKey(product.getProductId());
		}
		 return SUCCESS;
	}
	
	public String saveProduct(){
		try {
			if(StringUtils.isBlank(product.getProductId())) {
				product.setProductId(this.getNewUUID());
				product.setCreateTime(new Date());
				product.setUpdateTime(new Date());
				productService.addProduct(product);
			} else {
				product.setUpdateTime(new Date());
				productService.updateProduct(product);
			}
		} catch (Exception e) {
			loger.debug("保存订单有错！");
			loger.debug(e);
		}

		 return goToProductList();
	}
	
	public Product getProduct() {
		return product;
	}
	
	public void setProduct(Product product) {
		this.product = product;
	}
	
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	public List<Product> getProductList() {
		return productList;
	}
	public QueryPage getQueryPage() {
		return queryPage;
	}
	public void setQueryPage(QueryPage queryPage) {
		this.queryPage = queryPage;
	}
	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}
	
	
}